﻿using HighPrecisionTimer;
using Microsoft.Xaml.Behaviors.Core;
using Modules.ActionRun;
using Modules.Units;
using Prism.Mvvm;
using Prism.Regions;
using Sorting.App.Models.Enum;
using SortingWorkerService;
using System;
using System.Threading;
using System.Windows.Input;

namespace Modules.Monitoring.ViewModels
{
    public class ViewMonitoringViewModel : BindableBase
    {
        HPTimer hPTimer = new HPTimer();
        public ViewMonitoringViewModel()
        {
        }
        private bool startControlsEnabled = true;//开启开始按钮
        public bool StartControlsEnabled { get => startControlsEnabled; set => SetProperty(ref startControlsEnabled, value); }

        private bool exitControlsEnabled = false;//禁用退出按钮
        public bool ExitControlsEnabled { get => exitControlsEnabled; set => SetProperty(ref exitControlsEnabled, value); }
        private string ax0;//AX轴负极限
        public string Ax0 { get => ax0; set => SetProperty(ref ax0, value); }
        private string ax1;//AX轴正极限
        public string Ax1 { get => ax1; set => SetProperty(ref ax1, value); }
        private string axy;//AX轴原点
        public string Axy { get => axy;set => SetProperty(ref axy, value); }
        private string axs;//AX轴使能
        public string Axs { get => axs; set => SetProperty(ref axs, value); }
        private string az0;//AZ轴负极限
        public string Az0 { get => az0; set => SetProperty(ref az0, value); }
        private string az1;//AZ轴正极限
        public string Az1 { get => az1; set => SetProperty(ref az1, value); }
        private string azy;//AZ轴原点
        public string Azy { get => azy; set => SetProperty(ref azy, value); }
        private string azs;//AZ轴使能
        public string Azs { get => azs; set => SetProperty(ref azs, value); }
        private string bx0;//BX轴负极限
        public string Bx0 { get => bx0; set => SetProperty(ref bx0, value); }
        private string bx1;//BX轴正极限
        public string Bx1 { get => bx1; set => SetProperty(ref bx1, value); }
        private string bxy;//BX轴原点
        public string Bxy { get => bxy; set => SetProperty(ref bxy, value); }
        private string bxs;//BX轴使能
        public string Bxs { get => bxs; set => SetProperty(ref bxs, value); }
        private string bz0;//BZ轴负极限
        public string Bz0 { get => bz0; set => SetProperty(ref bz0, value); }
        private string bz1;//BZ轴正极限
        public string Bz1 { get => bz1; set => SetProperty(ref bz1, value); }
        private string bzy;//BZ轴原点
        public string Bzy { get => bzy; set => SetProperty(ref bzy, value); }
        private string bzs;//BZ轴使能
        public string Bzs { get => bzs; set => SetProperty(ref bzs, value); }
        private string ati;//A托盘入口传感器
        public string Ati { get => ati; set => SetProperty(ref ati, value); }
        private string atm;//A托盘中间传感器
        public string Atm { get => atm; set => SetProperty(ref atm, value); }
        private string ato;//A托盘出口传感器
        public string Ato { get => ato; set => SetProperty(ref ato, value); }
        private string bti;//B托盘入口传感器
        public string Bti { get => bti; set => SetProperty(ref bti, value); }
        private string btm;//B托盘中间传感器
        public string Btm { get => btm; set => SetProperty(ref btm, value); }
        private string bto;//B托盘出口传感器
        public string Bto { get => bto; set => SetProperty(ref bto, value); }
        private string scanImp;//扫码入口皮带传感器
        public string ScanImp { get => scanImp; set => SetProperty(ref scanImp, value); }
        private string scanMiddle;//扫码中间皮带传感器
        public string ScanMiddle { get => scanMiddle; set => SetProperty(ref scanMiddle, value); }
        private string scanOut;//扫码出口皮带传感器
        public string ScanOut { get => scanOut; set => SetProperty(ref scanOut, value); }
        private string dba;//挡板A
        public string Dba { get => dba; set => SetProperty(ref dba, value); }
        private string dbb;//挡板B
        public string Dbb { get => dbb; set => SetProperty(ref dbb, value); }
        private string turn;//转向皮带传感器
        public string Turn { get => turn; set => SetProperty(ref turn, value); }
        private string ac;//防撞传感器
        public string Ac { get => ac; set => SetProperty(ref ac, value); }
        private string safeAX;//托盘AX轴安全位置
        public string SafeAX { get => safeAX; set => SetProperty(ref safeAX, value); }
        private string safeAZ;//托盘AZ轴安全位置
        public string SafeAZ { get => safeAZ; set => SetProperty(ref safeAZ, value); }
        private string safeBZ;//托盘BZ轴安全位置
        public string SafeBZ { get => safeBZ; set => SetProperty(ref safeBZ, value); }
        private string safeBX;//托盘BX轴安全位置
        public string SafeBX { get => safeBX; set => SetProperty(ref safeBX, value); }
        /// <summary>
        /// 开始调试模式
        /// </summary>
        private ActionCommand startDebug;
        public ICommand StartDebug => startDebug ??= new ActionCommand(Start);
        private void Start()
        {
            StartControlsEnabled = false;//禁用开始按钮
            ExitControlsEnabled = true;//开启退出按钮
            Thread.Sleep(500);
            ActionRunHelper.SelectPlcModelType(EnumPlcModelType.Debug);//手动模式
            hPTimer.Interval = 200;
            hPTimer.Ticked += HPTimer_Ticked;
            hPTimer.Start();
        }
        private void HPTimer_Ticked()
        {
            Ax0 = ActionRunHelper.ReadAX0();
            Ax1 = ActionRunHelper.ReadAX1();
            Axy = ActionRunHelper.ReadAXY();
            Axs = ActionRunHelper.ReadAXS();
            Az0 = ActionRunHelper.ReadAZ0();
            Az1 = ActionRunHelper.ReadAZ1();
            Azy = ActionRunHelper.ReadAZY();
            Azs = ActionRunHelper.ReadAZS();
            Bx0 = ActionRunHelper.ReadBX0();
            Bx1 = ActionRunHelper.ReadBX1();
            Bxy = ActionRunHelper.ReadBXY();
            Bxs = ActionRunHelper.ReadBXS();
            Bz0 = ActionRunHelper.ReadBZ0();
            Bz1 = ActionRunHelper.ReadBZ1();
            Bzy = ActionRunHelper.ReadBZY();
            Bzs = ActionRunHelper.ReadBZS();
            Ati = ActionRunHelper.ReadATI();
            Atm = ActionRunHelper.ReadATM();
            Ato = ActionRunHelper.ReadATO();
            Bti = ActionRunHelper.ReadBTI();
            Btm = ActionRunHelper.ReadBTM();
            Bto = ActionRunHelper.ReadBTO();
            ScanImp = ActionRunHelper.ReadScanImp();
            ScanOut = ActionRunHelper.ReadScanOut();
            ScanMiddle = ActionRunHelper.ReadScanMiddle();
            Turn = ActionRunHelper.ReadTurn();
            Ac = ActionRunHelper.ReadAC();
            Dba = ActionRunHelper.ReadDBA();
            Dbb = ActionRunHelper.ReadDBB();
            SafeAX = ActionRunHelper.ReadSafeAX();
            SafeAZ = ActionRunHelper.ReadSafeAZ();
            SafeBX = ActionRunHelper.ReadSafeBX();
            SafeBZ = ActionRunHelper.ReadSafeBZ();
        }

        /// <summary>
        /// 退出调试模式
        /// </summary>
        private ActionCommand exitDebug;

        public ICommand ExitDebug => exitDebug ??= new ActionCommand(CloseDebug);

        private void CloseDebug()
        {
            StartControlsEnabled = true;//开启开始按钮
            ExitControlsEnabled = false;//禁用退出按钮
            hPTimer.Ticked -= HPTimer_Ticked;
            hPTimer.Stop();
            ActionRunHelper.SelectPlcModelType(EnumPlcModelType.Auto);//自动模式
        }

    }





}
